/*
* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1
*
* The contents of this file are subject to the Mozilla Public
* License Version 1.1 ("License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://www.zimbra.com/license
*
* Software distributed under the License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
* the License for the specific language governing rights and limitations
* under the License.
*
* The Original Code is: Zimbra AJAX Toolkit.
*
* The Initial Developer of the Original Code is Zimbra, Inc.
* Portions created by Zimbra are Copyright (C) 2005 Zimbra, Inc.
* All Rights Reserved.
*
* Portions of the original code have been adapted for CLDC 1.0 and MIDP 2.0 by CalemEAM Inc. 
* Portions created by CalemEAM are Copyright (C) 2008 CalemEAM Inc. All Rights Reserved.
*
* Contributor(s):
*
* ***** END LICENSE BLOCK *****
*/

package com.calemeam.text;

import java.util.Vector;

/**
 * MicroMessageFormat is simplifed to the following functions: <br>
 * format(String st, Vector v) where st is the string including position 
 * variables {n} corresponding to each vector element. <br>
 * For example, format("Hi {0}, I got {1} packages for you", v) where v is a vector of strings
 * of {"Jason", "5"}.
 */

public class MicroMessageFormat extends MicroFormat {
   
   public MicroMessageFormat(String pattern) {
      super(pattern);
      for (int i = 0; i < pattern.length(); i++) {
         // non-meta chars
         int head = i;
         char c;
         while(i < pattern.length()) {
            c = pattern.charAt(i);
            if (c == '{') {
               break;
            }
            i++;
         }
         int tail = i;
         if (head != tail) {
            MicroTextSegment segment = new MicroTextSegment(this, pattern.substring(head, tail));
            this.segments.addElement(segment);
            i--;
            continue;
         }

         // meta char
         head = i + 1;
         while(++i < pattern.length()) {
            if (pattern.charAt(i) == '}') {
               break;
            }		
         }
         tail = i;
         String field = pattern.substring(head, tail);
         MessageSegment segment = new MessageSegment(this, field);		
         this.segments.addElement(segment);
      }
   }
   //#if Debug
//#    public String toString() {
//#       return "[AjxMessageFormat: "+super.toString()+"]";
//#    }
   //#endif

   // Static methods
   public static String format(String pattern, Vector params) {
      return new MicroMessageFormat(pattern)._format(params);
   }

   //
   // AjxMessageFormat.MessageSegment class
   //
   protected class MessageSegment extends MicroSegment {
      private int index;
 
      public MessageSegment(MicroFormat format, String seg) {
         super(format, seg);
         this.index=Integer.parseInt(seg);
      }

      // Public methods
      public String format(Object ob) {
         Vector v=(Vector)ob;
         return (String)v.elementAt(this.index);
      }
   }
}
